package com.wandian.util
{
	import com.wandian.core.BitmapManager;
	import com.wandian.data.GameData;
	import com.wandian.view.stage.StageLoading;
	
	import flash.display.Bitmap;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.Dictionary;


	/**
	 * 并发下载器 ,只加载swf对象
	 * @author leo
	 *
	 */
	public class ConcurrentQueue extends EventDispatcher
	{
		/**
		 *  下载总数
		 */
		private var sumTotal:int=0;
		/**
		 * 已经完成的个数
		 */
		private var completeNum:int=0;
		/**
		 * 加载器列表
		 */
		private var loaderList:Dictionary;
		/**
		 * 并发量
		 */
		private var concurrentSessions:int=1;
		/**
		 * 需要加载的链接列表
		 */
		private var dataList:Array;
		/**
		 * 舞台loading界面
		 */
		private var stageLoading:StageLoading;
		/**
		 * 当前加载数
		 */
		private var curLoaderNum:int;

		public function ConcurrentQueue(dataList:Array, stageLoading:StageLoading, concurrentSessions:int=10)
		{
			this.sumTotal=dataList.length;
			this.loaderList=new Dictionary();
			this.concurrentSessions=concurrentSessions;
			this.dataList=dataList;
			this.stageLoading=stageLoading;
			//开始加载
			for (var i:int=0; i < concurrentSessions; i++)
			{
				start();
			}
		}

		/**
		 * 开始加载
		 */
		private function start():void
		{
			if (this.dataList && this.dataList.length > 0)
			{
				var url:String=dataList.shift();
				var loader:CustomLoader=new CustomLoader();
				loader.url=url;
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
				loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
				if (url.indexOf(".swf") != -1)
				{
					var loaderContext:LoaderContext=new LoaderContext(false, ApplicationDomain.currentDomain);
					loader.load(new URLRequest(url), loaderContext);
				}
				else
				{
					loader.load(new URLRequest(url));
				}
				this.loaderList[url]=loader;
				this.curLoaderNum++;
			}
		}

		/**
		 * 某一单项加载完成
		 * @param event
		 *
		 */
		private function completeHandler(event:Event):void
		{
			this.completeNum++;
			this.curLoaderNum--;
			var loader:CustomLoader=(((event.currentTarget) as LoaderInfo).loader as CustomLoader);
			if (loader.content is Bitmap)
			{
				var roleBitmapDict:Dictionary=BitmapManager.instance.roleBitmapDict;
				roleBitmapDict[loader.url]=((loader.content) as Bitmap).bitmapData;
				trace("缓存图片:"+loader.url);
			}
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
			loader.unload();
			var url:String=loader.url;
			this.loaderList[url]=null;
			delete this.loaderList[url];
			//设置加载进度
			var percent:int=this.completeNum / this.sumTotal * 100;
			this.stageLoading.setPercent(percent);
			//是否还需要加载
			if (this.dataList.length > 0 && this.curLoaderNum < this.concurrentSessions)
			{
				this.start();
			}
			//加载完成
			if (this.completeNum == this.sumTotal)
			{
				this.stageLoading.loadingComplete();
				this.loaderList=null;
			}
		}

		/**
		 * 加载出错
		 * @param event
		 *
		 */
		private function errorHandler(event:IOErrorEvent):void
		{
			var loader:CustomLoader=(((event.currentTarget) as LoaderInfo).loader as CustomLoader);
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler);
			loader.unloadAndStop();
			var url:String=loader.url;
			this.loaderList[url]=null;
			delete this.loaderList[url];
			trace("ConcurrentQueue 加载出错:"+loader.url);
		}
	}
}
